package model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Fourmiliere {

	private int foodStock;
	private int nbCadavre;
	private int posX;
	private int posY;
	private Ind_Fourmi reine;
	boolean reportBuilding;
	
	private ArrayList<Ind_Fourmi> fourmiList = new ArrayList<Ind_Fourmi>();
	private List<Ind_Fourmi> sFourmiList = Collections.synchronizedList(fourmiList);
	
	public Fourmiliere(int _X, int _Y,int _foodStock){
		posX = _X;
		posY = _Y;
		foodStock = _foodStock;
		
	}
	
	public int getFourmiListSize(){
		return sFourmiList.size();
	}

	public List<Ind_Fourmi> getFourmiList(){
		return sFourmiList;
	}
	
	public void setReine(Ind_Fourmi _reine){
		reine = _reine;
	}
	
	
	public synchronized boolean addFourmi(Ind_Fourmi _f){
		return sFourmiList.add(_f);
	}
	
	public synchronized boolean killFourmi(Ind_Fourmi _f){
		return sFourmiList.remove(_f);
	}
	
	public int getX(){
		return posX;
	}
	
	public int getFoodStock(){
		return foodStock;
	}
	
	public int eatFood(int quantity){
		if(foodStock>quantity){
			foodStock -= quantity;
			return quantity;
		}else{
			int res = quantity-foodStock;
			foodStock = 0;
			return res;
		}
	}
	
	public int getY(){
		return posY;
	}
	
	public void buildReport (Map<String,Object> report){
		ArrayList<Ind_Fourmi> cloneList = listClone();
		Iterator<Ind_Fourmi> it = cloneList.iterator();
		while(it.hasNext()){
			(it.next()).buildReport(report);
		}
	}
	
	public int fourmiListSize(){
		return sFourmiList.size();
	}
	
	public int getNbCadavre(){
		return nbCadavre;
	}
	

	
	public  void updateFourmi(){
		
		ArrayList<Ind_Fourmi> cloneList = listClone();
		Iterator<Ind_Fourmi> it = cloneList.iterator();
		while(it.hasNext()){
				(it.next()).update();			
		}
	}
	
	public synchronized ArrayList<Ind_Fourmi> listClone(){
		return new ArrayList<Ind_Fourmi>(sFourmiList);
	}
	
	public boolean reineAlive(){
		return reine!=null;
	}

	public synchronized ArrayList<Ind_Fourmi> listCloneAtHome(){
		ArrayList<Ind_Fourmi> res = listClone();
		Iterator<Ind_Fourmi> it = res.iterator();
		while(it.hasNext()){
				if(!it.next().atHome()) it.remove();			
		}
		return res;
	}
	
}
